package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author 鹏少
 * @Date 2023/9/22
 * @Date 15:13
 */
@Slf4j
@Component
public class OrderTask {
    @Autowired
    private OrderMapper orderMapper;

    @Scheduled(cron = "0 * * * * ?")
    public void processTimeoutOrder(){
        log.info("处理超时支付的订单");
        LocalDateTime time = LocalDateTime.now().minusMinutes(15);
        List<Orders> ordersList=orderMapper.getByStatusAndOrdertime(Orders.PENDING_PAYMENT,time);

        if (ordersList!=null||ordersList.size()>0){

            for (Orders orders : ordersList) {
                orders.setStatus(Orders.CANCELLED);
                orders.setCancelReason("订单超时,系统自动取消");
                orders.setCancelTime(LocalDateTime.now());
                orderMapper.update(orders);
            }

        }


    }

    @Scheduled(cron = "0 0 1 * * ?")
    public void processDeliveryOrder(){
        log.info("处理处于派送中的订单");

        LocalDateTime time = LocalDateTime.now().minusHours(1);
        List<Orders> ordersList=orderMapper.getByStatusAndOrdertime(Orders.DELIVERY_IN_PROGRESS,time);

        if (ordersList!=null||ordersList.size()>0){

            for (Orders orders : ordersList) {
                orders.setStatus(Orders.COMPLETED);
                orders.setDeliveryTime(LocalDateTime.now());
                orderMapper.update(orders);
            }

        }



    }

}
